package com.bookanalysis.activity;

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.alex.books.BaseBook;
import com.alex.books.BookPageFactory;
import com.alex.exception.UnsupportedFormatException;

public class TestBookAnalysisActivity extends Activity implements
		OnClickListener {
	/** Called when the activity is first created. */
	private final String TAG = "TestBookAnalysisActivity";

	private TextView mContentTV;
	private Button mPreviousPageBT;
	private Button mNextPageBT;
	private Button mPreviousChapterBT;
	private Button mNextChapterBT;
	private Button mIncreaeSizeBT;
	private Button mDecreaeSizeBT;
	private Button mIncreaeSpaceBT;
	private Button mDecreaeSpaceBT;
	float density;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_analysis);

		mContentTV = (TextView) findViewById(R.id.content);

		mPreviousPageBT = (Button) findViewById(R.id.previous_page);
		mPreviousPageBT.setOnClickListener(this);

		mNextPageBT = (Button) findViewById(R.id.next_page);
		mNextPageBT.setOnClickListener(this);

		mPreviousChapterBT = (Button) findViewById(R.id.previous_chapter);
		mPreviousChapterBT.setOnClickListener(this);

		mNextChapterBT = (Button) findViewById(R.id.next_chapter);
		mNextChapterBT.setOnClickListener(this);

		mIncreaeSizeBT = (Button) findViewById(R.id.increase_size);
		mIncreaeSizeBT.setOnClickListener(this);

		mDecreaeSizeBT = (Button) findViewById(R.id.decrease_size);
		mDecreaeSizeBT.setOnClickListener(this);

		mIncreaeSpaceBT = (Button) findViewById(R.id.increase_space);
		mIncreaeSpaceBT.setOnClickListener(this);

		mDecreaeSpaceBT = (Button) findViewById(R.id.decrease_space);
		mDecreaeSpaceBT.setOnClickListener(this);

		BaseBook base = new BaseBook();
		base.mIsFirstReader = 1;
		base.mBookPath = "/sdcard/sd/test_b.txt";
		//BookPageFactory.getInstance().geteratedCatalog2(this,base, "章");
		
		try {
			BookPageFactory.getInstance().openBook(this, base);
		} catch (UnsupportedFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);
        int width = metric.widthPixels;  // 屏幕宽度（像素）
        int height = metric.heightPixels;  // 屏幕高度（像素）
        density = metric.density;  // 屏幕密度（0.75 / 1.0 / 1.5）
        int densityDpi = metric.densityDpi;  // 屏幕密度DPI（120 / 160 / 240）
        
		Display display = getWindowManager().getDefaultDisplay();

		int mScreenW = display.getWidth();
		int mScreenH = display.getHeight();
		BookPageFactory.getInstance().setReaderFormat(24*density, 0, mScreenW, mScreenH-150);
		mContentTV.setTextSize(24);
		mContentTV.setText(BookPageFactory.getInstance().nextPage());
		
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		int v_id = v.getId();
		switch (v_id) {
		case R.id.previous_page:
			if (BookPageFactory.getInstance().isFirstPage()) {
				Toast.makeText(this, "已经是第一页了", Toast.LENGTH_SHORT).show();
			}else{
				mContentTV.setText(BookPageFactory.getInstance().previousPage());
			}
			
			break;
		case R.id.next_page:
			if(BookPageFactory.getInstance().isLastPage()){
				Toast.makeText(this, "已经是最后一页了", Toast.LENGTH_SHORT).show();
			}else{
				mContentTV.setText(BookPageFactory.getInstance().nextPage());
			}
			
			break;
		case R.id.previous_chapter:
			if(BookPageFactory.getInstance().isFirstChapter()){
				Toast.makeText(this, "已经是第一章了", Toast.LENGTH_SHORT).show();
			}else{
				mContentTV.setText(BookPageFactory.getInstance().previousChapter());
			}
			break;
		case R.id.next_chapter:
			if(BookPageFactory.getInstance().isLastChapter()){
				Toast.makeText(this, "已经是最后一章了", Toast.LENGTH_SHORT).show();
			}else{
				mContentTV.setText(BookPageFactory.getInstance().nextChapter());
			}
			
			break;
		case R.id.increase_size:
			BookPageFactory.getInstance().setFontSize(30*density);
			mContentTV.setTextSize(30);
			mContentTV.setText(BookPageFactory.getInstance().refreshPage());
			break;
		case R.id.decrease_size:
			BookPageFactory.getInstance().setFontSize(24*density);
			mContentTV.setTextSize(24);
			mContentTV.setText(BookPageFactory.getInstance().refreshPage());
			break;
		case R.id.increase_space:
			space += 3;
			mContentTV.setLineSpacing(space,1);
			BookPageFactory.getInstance().setLineSpace(space);
			mContentTV.setText(BookPageFactory.getInstance().refreshPage());
			break;
		case R.id.decrease_space:
			space -= 3;
			mContentTV.setLineSpacing(space,1);
			BookPageFactory.getInstance().setLineSpace(space);
			mContentTV.setText(BookPageFactory.getInstance().refreshPage());
			break;

		default:
			break;
		}
	}
	int space = 0;

}